package chain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import jxl.Sheet;
import jxl.Workbook;
import termWeighting.PrepareTF;
import termWeighting.ProposedTermWeighting;
import type.TestTWtype;
import type.TfType;
import util.BugListProcess;
import util.MapUtil;
import dataProcess.ForLPU;
import dataProcess.ReadExcel;
import dataProcess.StringSpliter;
import ds.BugInfo;
import ds.Constants;
import ds.BugInfo;

public class runNTMinChain {
	public static void main(String args[]) throws Exception {	
		/**categories*/
		/*String[] originCategories = { "Functionality", "Security", "Usability", "Performance",
				"Security Defect", "UI", "Efficiency"};*/
		String[] originCategories = {"Critical", "High", "Medium", "Low"};	
		HashMap<String, String> mergeMap = new HashMap<String, String>();
		mergeMap.put("Fatal", "Critical");
		mergeMap.put("Serious", "High");
		/**text indexes in Excel file*/
		//int[] textIndexes1 = {10};
		int[] textIndexes2 = {1, 8};
		//int[] textIndexes4 = {3, 4};
		/**different data resource*/
		ReadExcel bugGetter = new ReadExcel(originCategories);
		ArrayList<BugInfo> allBugs = new ArrayList<BugInfo>();
		//allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports1.xls",  textIndexes1, 8));//MS 1
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports2.xls", textIndexes2, 7));//MS 2
		//allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports4.xls", textIndexes4, 12));// Android 
		//allBugs.addAll(bugGetter.getSecurityBugs("data/input/51security"));//Mozilla
		//allBugs.addAll(bugGetter.getDaiBugs());//Dai's bugs
		/**merge label*/
		ArrayList<String> labelList = bugGetter.getLabels();
		labelList.removeAll(mergeMap.keySet());
		/**merge bugs with same category and divide test bugs*/
		BugListProcess blp = new BugListProcess(allBugs, labelList);
		blp.run(mergeMap, 0.3);
		/**data process for NTM*/
		PrepareTF.run(Constants.trainDirForNTM, Constants.testDirForNTM, "output/NTM/TF", "output/NTM/wordlist", TfType.pureTF);	
		ProposedTermWeighting TWcalculator = new ProposedTermWeighting(
				labelList, MapUtil.getGroundTruth(allBugs),"output/NTM/TF", blp.getClassProbablity());	
		double[] params = {2.0, 4.0};
		TWcalculator.setParams(params);
		
		ArrayList<BugInfo> trainDocs = TWcalculator.caculateTermWeightForTrain(TfType.pureTF);
		util.WekaDataWrapper.translateToArffTF(trainDocs, labelList, "output/NTM/wordlist", "output/NTM/train.arff");
		
		ArrayList<BugInfo> testDocs_D = TWcalculator.calculateTermWeightForTest(TestTWtype.D_Max,TfType.pureTF);	
		util.WekaDataWrapper.translateToArffTF(testDocs_D, labelList, "output/NTM/wordlist", "output/NTM/test_D.arff");
		
		ArrayList<BugInfo> testDocs_W = TWcalculator.calculateTermWeightForTest(TestTWtype.W_Max,TfType.pureTF);	
		util.WekaDataWrapper.translateToArffTF(testDocs_W, labelList, "output/NTM/wordlist", "output/NTM/test_W.arff");
		
		ArrayList<BugInfo> testDocs_DT = TWcalculator.calculateTermWeightForTest(TestTWtype.D_TMax,TfType.pureTF);	
		util.WekaDataWrapper.translateToArffTF(testDocs_DT, labelList, "output/NTM/wordlist", "output/NTM/test_DT.arff");
		
		/**NB
		 */
		TWcalculator.predictUseNB("output/NB/NBresult");
			
		/**LPU
		/*calculate TF for train and test
		**/
		PrepareTF.run(Constants.trainDirForLPU, Constants.testDirForLPU,
				"output/LPU/TfFile", "output/LPU/wordList", TfType.pureTF);
		
		for(String curLabel : labelList){
			if(!curLabel.equals("")){
				MapUtil.savePosId(Constants.posIDmap, curLabel, allBugs);
				try {
					double[] posRates = { 0.3, 0.4, 0.5, 0.6, 0.7 };
					ForLPU lpu = new ForLPU();
					// lpu.runWithOriginUnlabel();
					for (double posRate : posRates)
						lpu.runWithSameTest(curLabel + posRate, posRate);
					System.out.print("LPU for same test completed!");
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		}
	}
}
